/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: WaybillController
 * Author:   李毅
 * Date:     2019/10/18 9:20
 * Description: 配车控制类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.ymtms.supplycar.controller;

import com.github.pagehelper.PageHelper;
import com.ymtms.supplycar.pojo.DriverInformation;
import com.ymtms.supplycar.pojo.GoodsbillMaster;
import com.ymtms.supplycar.pojo.GoodsbillReceiptDetailed;
import com.ymtms.supplycar.pojo.Waybill;
import com.ymtms.supplycar.service.*;
import com.ymtms.supplycar.vo.GridRequest;
import com.ymtms.supplycar.vo.GridResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 〈一句话功能简述〉<br> 
 * 〈配车控制类〉
 *
 * @author 李毅
 * @create 2019/10/18
 * @since 1.0.0
 */
@RestController
@CrossOrigin
public class WaybillController {

    @Autowired
    GoodsbillMasterService goodsbillMasterService;

    @Autowired
    WaybillService waybillService;

    @Autowired
    GoodsbillReceiptDetailedService goodsbillReceiptDetailedService;

    @Autowired
    DriverInformationService driverInformationService;

    @Autowired
    GoodsbillEventService goodsbillEventService;

    @ApiOperation(value = "插入运输单",notes = "根据货运单id创建运输单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gmid", value = "货运单主键",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "wagmidybill",value = "运输单实体",required = true,dataType = "Waybill")})
    @RequestMapping(value = "/insertinfo",method = RequestMethod.POST)
    public GridResult insertMessage(@RequestParam("gmid") Integer gmid, Waybill waybill
                                    ,String startCarryTime,String endCarryTime){
        GridResult result = new GridResult();
        if(gmid == null || waybill==null){
            result.setCode(500);
            result.setMsg("参数传递为空，请重试！");
            return result;
        }
        try {
            //根据gid去查货运信息
            GoodsbillMaster goodsbill = goodsbillMasterService.findGoodsbillByGid(gmid);
            waybill.setGoodsBillpkid(gmid);
            //根据gid查询货运回执单信息
            GoodsbillReceiptDetailed goodsbillReceipt = goodsbillReceiptDetailedService.findGoodsbillReceiptDetailedGmid(gmid);
            BigDecimal weight = goodsbillReceipt.getWeight();
            BigDecimal volume = goodsbillReceipt.getVolume();
            //根据重量、体积、载重
            DriverInformation driver = driverInformationService.findDriver(weight, volume);
            waybill.setDriverPkid(driver.getDiid());
            waybill.setGoodsBillpkid(gmid);
            waybill.setContractCode(UUID.randomUUID().toString());
            waybill.setTotalCarriage(goodsbill.getCarriage().add(goodsbill.getCarryGoodsFee()));
            waybill.setInsurance(goodsbill.getInsurance());
            waybill.setFlag(0);
            waybillService.insertWaybill(waybill);
            //改变状态
            goodsbillEventService.updateEvent(gmid);
            result.setMsg("添加成功！");
            result.setCode(0);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Map getAll(){
        Map map = new HashMap();
        String state = "已审核";
        try {
            List<GoodsbillMaster> goodsbills = goodsbillMasterService.findAllByState(state);
            map.put("data",goodsbills);
            map.put("code",0);
            map.put("msg","查询成功");
        }catch (Exception e){
            map.put("msg",e.getMessage());
            map.put("code",500);
        }
        return map;
    }










    @RequestMapping(value = "/getGoodsbillMaster",method = RequestMethod.GET)
    public GridResult getGoodsbillMaster(@RequestParam(value = "page",required = false) Integer page,
                                         @RequestParam(value = "limit",required = false) Integer limit){
        GridRequest gridRequest = new GridRequest();
        if (page!=null && limit!=null){
            gridRequest.setLimit(limit);
            gridRequest.setPage(page);
        }
        GridResult result = new GridResult();
        String state = "已审核";
//        try {
            List<GoodsbillMaster> goodsbills = goodsbillMasterService.findGoodsbillByState(state,gridRequest);
            result.setCount(goodsbillMasterService.findCount(state));
            result.setCode(0);
            result.setData(goodsbills);
            result.setMsg("查询成功");
//        }catch (Exception e){
//            result.setCode(500);
//            result.setMsg(e.getMessage());
//        }
        return result;
    }

    @RequestMapping(value = "/getWaybill",method = RequestMethod.GET)
    public GridResult getWaybill(GridRequest gridRequest,Waybill conditionWaybill,String date){
        GridResult result = new GridResult();
        String contractCode = conditionWaybill.getContractCode();
        try {
            List<Waybill> waybills = waybillService.selectAll(gridRequest,contractCode,date);
            for (Waybill waybill : waybills) {
                waybill.setDriver(driverInformationService.getDriver(waybill.getDriverPkid()));
            }
            result.setData(waybills);
            result.setCode(0);
            result.setMsg("查询成功");
            result.setCount(waybillService.selectTotalCount());
        }catch (Exception e){
            result.setCode(500);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/deleteWaybill")
    public GridResult deleteWaybill(@RequestParam("wid") Integer wid) {
        GridResult result = new GridResult();
        if (wid==0){
            result.setCode(500);
            result.setMsg("参数传递为空，请重试！");
            return result;
        }
        try {
            waybillService.deleteWaybillByWid(wid);
            result.setCount(0);
            result.setMsg("删除成功！");
        } catch (Exception e) {
            result.setCode(500);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/updateWaybill",method = RequestMethod.POST)
    public GridResult updateWaybill(Waybill waybill){
        GridResult result = new GridResult();
        if (waybill == null){
            result.setCode(500);
            result.setMsg("参数传递为空，请重试！");
            return result;
        }
        try {
            waybillService.updateWaybillByWid(waybill);
            result.setCode(0);
            result.setMsg("修改成功！");
        }catch (Exception e){
            result.setCode(500);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    /**
     * @Description 根据职员账号login_id查询该职员所负责的货运单
     * @Param login_id 职员账号
     * @return map
     * @date   2019/11/6  9:25
     * @author 张泓
     */
    @RequestMapping(value = "/getGoodsbillMasterByLoginId",method = RequestMethod.GET)
    public Map getGoodsbillMasterByLoginId(@RequestParam(value = "login_id",required = false) String login_id){
        Map map = new HashMap();
        try {
            List<GoodsbillMaster> goodsbills = goodsbillMasterService.getGoodsbillMasterByLoginId(login_id);
            map.put("data",goodsbills);
            map.put("code",0);
            map.put("msg","查询成功");
        }catch (Exception e){
            map.put("msg",e.getMessage());
            map.put("code",500);
        }
        return map;
    }

    /**
     * @Description 根据货运单编号gmid查询相应的运输单
     * @Param gmid 货运单编号
     * @return Waybill 实体
     * @date   2019/11/6  10:09
     * @author 张泓
     */
    @RequestMapping(value = "/selectWaybillByGmid",method = RequestMethod.GET)
    public Map selectWaybillByGmid(@RequestParam(value = "gmid",required = false) Integer gmid){
        Map map = new HashMap();
        try {
            Waybill waybill = waybillService.selectWaybillByGmid(gmid);
            map.put("data",waybill);
            map.put("code",0);
            map.put("msg","查询成功");
        }catch (Exception e){
            map.put("msg",e.getMessage());
            map.put("code",500);
        }
        return map;
    }

}